package com.groupbuying.settleproducer.service.impl;

import com.alibaba.fastjson.JSON;
import com.groupbuying.settleproducer.connector.SysconfigFeign;
import com.groupbuying.settleproducer.connector.YSFeign;
import com.groupbuying.settleproducer.constants.SettleYsSplitRequestReceiptEnumConstants;
import com.groupbuying.settleproducer.dao.SettleUshopYSSplitRequestReceiptDao;
import com.groupbuying.settleproducer.domain.SettleUshopYSSplitRequestReceiptDO;
import com.groupbuying.settleproducer.service.SettleUshopYSSplitRequestReceiptService;
import com.groupbuying.settleproducer.utils.DateUtils;
import com.groupbuying.settleproducer.utils.JsonUtils;
import com.groupbuying.settleproducer.utils.apiresult.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: WANGNING
 * @Email wyn20140226@aliyun.com
 * @Date: 2019/5/10 16:50
 * @Des 银盛分账查询业务处理
 */
@Service
public class SettleUshopYSSplitRequestReceiptServiceImpl implements SettleUshopYSSplitRequestReceiptService {
    public static final Logger logger = LoggerFactory.getLogger(SettleUshopYSSplitRequestReceiptServiceImpl.class);
    @Autowired
    private SettleUshopYSSplitRequestReceiptDao ysSplitRequestReceiptDao;
    @Autowired
    private YSFeign ysFeign;
    @Autowired
    private SysconfigFeign sysconfigFeign;
    @Override
    public SettleUshopYSSplitRequestReceiptDO get(String id) {
        return this.ysSplitRequestReceiptDao.get(id);
    }

    @Override
    public List<SettleUshopYSSplitRequestReceiptDO> list(Map<String, Object> map) {

        return this.ysSplitRequestReceiptDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {

        return this.ysSplitRequestReceiptDao.count(map);
    }

    @Override
    public int save(SettleUshopYSSplitRequestReceiptDO splitRequestReceipt) {

        return this.ysSplitRequestReceiptDao.save(splitRequestReceipt);
    }

    @Override
    public int update(SettleUshopYSSplitRequestReceiptDO splitRequestReceipt) {

        return this.ysSplitRequestReceiptDao.update(splitRequestReceipt);
    }

    @Override
    public int remove(String id) {

        return this.ysSplitRequestReceiptDao.remove(id);
    }

    @Override
    public int batchRemove(String[] ids) {

        return this.ysSplitRequestReceiptDao.batchRemove(ids);
    }

    @Override
    public ApiResult createRequestReceipt(Map<String, Object> mapParams) {
        try {
            // 解析map参数
            Object tempParam = mapParams.get("outTradeNo");
            String outTradeNo = tempParam == null ? null : tempParam.toString();
            //获取原交易发起方
            String srcUsercode =this.sysconfigFeign.getPayeeUsercode();
            //判断请求参数不为空
            if (StringUtils.isEmpty(outTradeNo)&&StringUtils.isEmpty(srcUsercode)) {
                logger.info("SettleUshopYSSplitRequestReceiptServiceImpl.createRequestReceipt()", "原交易发起方或原订单号有误,订单号： "+outTradeNo);
                return ApiResult.paramIsNull();
            }
            Map<String,Object> map=new HashMap<>();
            map.put("out_trade_no",outTradeNo);
            map.put("src_usercode",srcUsercode);
            String requestReceiptJson= JsonUtils.toJSon(map);
            Map<String,Object> condMap=new HashMap<>();
            condMap.put("biz_content", requestReceiptJson);
            logger.info("SettleUshopYSSplitRequestReceiptServiceImpl.createRequestReceipt()", "调用银盛请求回执之前的参数biz_content数据为："+condMap.get("biz_content"));
            String feignMapStr=this.ysFeign.splitRequestReceipt(condMap);
            logger.info("SettleUshopYSSplitRequestReceiptServiceImpl.createRequestReceipt()", "调用银盛请求回执返回的参数json数据为："+feignMapStr);
            Map feignMap=JsonUtils.jsonToMap(feignMapStr);
            SettleUshopYSSplitRequestReceiptDO ysSplitResquestReturn=new SettleUshopYSSplitRequestReceiptDO();
            ysSplitResquestReturn.setCode((String) feignMap.get("code"));
            ysSplitResquestReturn.setMsg((String) feignMap.get("msg"));
            ysSplitResquestReturn.setSrcUsercode((String) feignMap.get("src_usercode"));
            ysSplitResquestReturn.setTradeDate((String) feignMap.get("trade_date"));
            ysSplitResquestReturn.setOutTradeNo((String) feignMap.get("out_trade_no"));
            ysSplitResquestReturn.setOutBatchNo(DateUtils.format(new Date(), "yyyyMMdd"));
            ysSplitResquestReturn.setDivisionMode((String) feignMap.get("division_mode"));
            ysSplitResquestReturn.setTotalAmount(new BigDecimal((String)feignMap.get("total_amount")));
            ysSplitResquestReturn.setFee(new BigDecimal((String) feignMap.get("fee")));
            ysSplitResquestReturn.setFeePayer((String) feignMap.get("fee_payer"));
            ysSplitResquestReturn.setDivisionStatus((String) feignMap.get("division_status"));
            ysSplitResquestReturn.setDivisionStatusCode((String) feignMap.get("division_status_code"));
            ysSplitResquestReturn.setNote((String) feignMap.get("note"));
            List list=(List) feignMap.get("detailResults");
            String str= JSON.toJSON(list).toString();
            ysSplitResquestReturn.setDetailResults(str);
            ysSplitResquestReturn.setGmtCreate(new Date());
            ysSplitResquestReturn.setGmtModified(new Date());
            ysSplitResquestReturn.setIsDelete(SettleYsSplitRequestReceiptEnumConstants.ENUM_ISDELETE_UNDEL);
            ysSplitRequestReceiptDao.save(ysSplitResquestReturn);
            logger.info("SettleUshopYSSplitRequestReceiptServiceImpl.createRequestReceipt()", "保存银盛请求回执成功！");
            return ApiResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("SettleUshopYSSplitRequestReceiptServiceImpl.createRequestReceipt()", "银盛请求回执异常！"+e);
        }
        return ApiResult.failure();
    }

}
